/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.pa165languageschoolclient;

import com.pa165.languageschoolservice.DTO.LectureDTO;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Elena Medvedeva
 */
public class ClientLecture {
    
    public static final String LECTURES = "lectures";
    public static final String GET_ALL_LECTURES = "getAllLectures";    
    public static final String GET_LECTURE = "getLecture";
    public static final String GET_LECTURES_BY_COURSE_ID = "getLecturesByCourse";
    public static final String ADD_LECTURE = "createLecture";
    public static final String MODIFY_LECTURE = "updateLecture";
    public static final String DELETE_LECTURE = "deleteLecture";
    
    private Client client;
    private String url;
 
    
    public ClientLecture(String url){
        client = AppClient.getClient();
        this.url = url;
      
    }
    
    public String getLectureURL() {
        return url + LECTURES;
    }
    
    public List<LectureDTO> getAllLectures() {
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getLectureURL() + "/" + GET_ALL_LECTURES);

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

        if (response.getStatus() != 200) {
            System.out.println("Request  failed: " + response.getEntity(String.class));
            return null;
        }
        GenericType<List<LectureDTO>> type = new GenericType<List<LectureDTO>>() {
        };
        List<LectureDTO> output = response.getEntity(type);
        return output;
    }

    public LectureDTO getLecture(int id){
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getLectureURL() + "/" + GET_LECTURE + "?id=" + id);

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);
              

        if (response.getStatus() != 200) {
            if (response.getStatus() == 404) {
                System.out.println("Couldn't find lecture with id=" + id);

            } else {
                System.out.println("Request  failed: " + response.getEntity(String.class));
            }
            return null;
        }

        LectureDTO output = response.getEntity(LectureDTO.class);       
        return output;
    }
    
    public List<LectureDTO> getLecturesByCourse(int id){
        client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getLectureURL() + "/" + GET_LECTURES_BY_COURSE_ID + "?id=" + id);

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                .get(ClientResponse.class);

         if (response.getStatus() != 200) {
            if (response.getStatus() == 404) {
                System.out.println("Couldn't find course with id=" + id);

            } else {
                System.out.println("Request  failed: " + response.getEntity(String.class));
            }
            return null;
        }
        GenericType<List<LectureDTO>> type = new GenericType<List<LectureDTO>>() {
        };
        List<LectureDTO> output = response.getEntity(type);
        return output;
    }
    
     public boolean addLecture(LectureDTO lecture) {
         client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getLectureURL() + "/" + ADD_LECTURE);
        
        ClientResponse cr = webResource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, lecture);
         int status = cr.getStatus();
         if ((status >= 200) && (status <= 299)) {
             System.out.println("status=: " + status);
            return true;
        } else {
            System.out.println("status=: " + status);
            System.out.println("Cluster creation failed: " + cr.getEntity(String.class));
            return false;
        }
       
    }
     
        public boolean modifyLecture(LectureDTO lecture){
            client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
          WebResource webResource = client
                .resource(getLectureURL() + "/" + MODIFY_LECTURE);

        ClientResponse cr = webResource.type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .post(ClientResponse.class, lecture);
         int status = cr.getStatus();
         if ((status >= 200) && (status <= 299)) {
             System.out.println("status=: " + status);
            return true;
        } else {
            System.out.println("Cluster creation failed: " + cr.getEntity(String.class));
            return false;
        }
     }
     
     public boolean deleteLecture(int id) {
         client.addFilter(new HTTPBasicAuthFilter(CommandLineClient.DEFAULT_USER, CommandLineClient.DEFAULT_PASSWORD));
        WebResource webResource = client
                .resource(getLectureURL() + "/" + DELETE_LECTURE + "?id=" + id);

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                .delete(ClientResponse.class);
        int status = response.getStatus();
        if (status == 200) {
            return true;
        } else {
            if (status == 404) {
                System.out.println("Couldn't find lecture with id=" + id);
            } else {
                System.out.println("Request  failed: " + response.getEntity(String.class));
            }            
            return false;
        }
    }
}
